package com.campuscarbooking.api.dto.response;

import com.campuscarbooking.api.model.BusSchedule;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.time.LocalTime;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ScheduleResponse {
    private Long id;
    private LocalTime departureTime;
    private LocalTime arrivalTime;
    private String routeDirection;
    private boolean isFixed;
    private int capacity;
    private int bookedSeats;
    private int availableSeats;
    private BigDecimal price;
    private String status;
    private DriverDto driver;

    public static ScheduleResponse fromBusSchedule(BusSchedule schedule) {
        if (schedule == null) {
            return null;
        }

        // Calculate available seats
        int bookedSeats = 0; // This would need to be calculated from reservations
        int availableSeats = schedule.getCapacity() - bookedSeats;

        return ScheduleResponse.builder()
                .id(schedule.getId())
                .departureTime(schedule.getDepartureTime())
                .arrivalTime(schedule.getArrivalTime())
                .routeDirection(schedule.getRouteDirection().name())
                .isFixed(schedule.getIsFixed())
                .capacity(schedule.getCapacity())
                .bookedSeats(bookedSeats)
                .availableSeats(availableSeats)
                .price(schedule.getPrice())
                .status(schedule.getStatus().name())
                .build();
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class DriverDto {
        private Long id;
        private String name;
        private String phone;
        private String licensePlate;
        private BigDecimal rating;
    }
}